package com.jingdianjichi.subject.application.service.impl;

import com.jingdianjichi.subject.application.service.SubjectCategoryApplicationService;
    import com.jingdianjichi.subject.application.convert.SubjectCategoryBOConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdinajichi.subject.common.enmus.CategoryTypeEnum;
import com.jingdinajichi.subject.common.enmus.IsDeleteCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zwy
 * @version 1.0
 * @description: SubjectCategoryApplicationServiceImpl
 * @date 2025/10/4 14:06
 */
@Service
@Slf4j
public class SubjectCategoryApplicationServiceImpl implements SubjectCategoryApplicationService {
    @Resource
    private SubjectCategoryService subjectCategoryService;
    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryApplicationServiceImpl.add.bo:{}",subjectCategoryBO);
        }
        SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询岗位大类
     */
    @Override
    public List<SubjectCategoryBO> queryPrimaryCategory(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryApplicationServiceImpl.queryPrimaryCategory");
        }
        SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        List<SubjectCategory> subjectCategories = subjectCategoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryBO> subjectCategoryBOS = SubjectCategoryBOConverter.INSTANCE.convertCategoryListToCategoryBOList(subjectCategories);
        return subjectCategoryBOS;

    }
    /**
     * 查询题目分类小类
     */
    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {

        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryApplicationServiceImpl.queryCategoryByParentId.BO:{}",subjectCategoryBO);
            }

            SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
            subjectCategory.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
            subjectCategory.setCategoryType(CategoryTypeEnum.SECOND.getCode());
            List<SubjectCategory> subjectCategories = subjectCategoryService.queryCategory(subjectCategory);
            List<SubjectCategoryBO> subjectCategoryBOS = SubjectCategoryBOConverter.INSTANCE.convertCategoryListToCategoryBOList(subjectCategories);
            return subjectCategoryBOS;
        }catch (Exception e){
            log.error("SubjectCategoryApplicationServiceImpl.queryCategoryByParentId.error",e);
            throw e;
        }

    }
    /**
     * 更新分类
     */
    /**
     * 更新分类
     *
     * @param subjectCategoryBO
     */
    @Override
    public boolean update(SubjectCategoryBO subjectCategoryBO) {
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryApplicationServiceImpl.update.BO:{}",subjectCategoryBO);
            }
            SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
            int num = subjectCategoryService.update(subjectCategory);
            return num > 0;
        }catch (Exception e){
            log.error("SubjectCategoryApplicationServiceImpl.update.error",e);
            return false;
        }
    }
    /**
     * 删除分类
     */
    /**
     * 删除分类
     *
     * @param subjectCategoryBO
     */
    @Override
    public boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory category = SubjectCategoryBOConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        category.setIsDeleted(IsDeleteCodeEnum.DELETE_CODE_ENUM.getCode());
        return subjectCategoryService.update( category) > 0;
    }
}
